package com.lemon.boot.controller;

import com.lemon.boot.common.base.BaseController;
import com.lemon.boot.dto.TenantMenuDTO;
import com.lemon.boot.dto.TenantMenuResourceDTO;
import com.lemon.boot.po.TenantMenuPO;
import com.lemon.boot.service.ITenantMenuService;
import com.lemon.boot.vo.LeftMenuVO;
import com.lemon.boot.vo.TenantMenuResourceVO;
import com.lemon.boot.vo.TreeSelectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.List;
import java.util.Map;

/**
 * 租户-菜单中间表控制层
 *
 * @author Lemon
 * @since 2024-02-29
 */
@Log4j2
@Api(tags = "租户-菜单关联")
@RestController
@RequestMapping("tenantMenu")
public class TenantMenuController extends BaseController<TenantMenuPO, TenantMenuDTO, ITenantMenuService> {
    @Resource
    private ITenantMenuService iTenantMenuService;

    /**
     * 查询左侧菜单
     *
     * @return
     */
    @PermitAll
    @ApiOperation(value = "查询左侧菜单")
    @GetMapping("getLeftMenu")
    public List<LeftMenuVO> getLeftMenu() {
        return iTenantMenuService.getLeftMenu();
    }

    /**
     * 排序-上移/下移
     *
     * @return
     */
    @ApiOperation(value = "排序")
    @PutMapping("order")
    public void order(@RequestBody Map<String, Object> param) {
        iTenantMenuService.order(param);
    }

    /**
     * 查询当前租户所有的菜单及资源
     *
     * @return
     */
    @PreAuthorize("hasRole('ROLE_SUPER')")
    @ApiOperation(value = "查询当前租户所有的菜单及资源")
    @GetMapping("getAllMenuResourceByCurrent")
    public List<TenantMenuResourceVO> getAllMenuResourceByCurrent() {
        return iTenantMenuService.getAllMenuResourceByCurrent();
    }

    /**
     * 获取树形选择数据-包括父菜单
     *
     * @return
     */
    @ApiOperation(value = "获取树形选择数据-包括父菜单")
    @GetMapping("getParentByTreeSelect")
    public List<TreeSelectVO> getParentByTreeSelect() {
        return iTenantMenuService.getParentByTreeSelect();
    }

    /**
     * 给租户分配菜单
     *
     * @return
     */
    @PreAuthorize("hasRole('ROLE_SUPER')")
    @ApiOperation(value = "给租户分配菜单")
    @PutMapping("addMenuResourceByTenantId")
    public String addMenuResourceByTenantId(@RequestBody TenantMenuResourceDTO tenantMenuResourceDTO) {
        return iTenantMenuService.addMenuResourceByTenantId(tenantMenuResourceDTO);
    }

    /**
     * 根据租户ID查询菜单
     *
     * @return
     */
    @PreAuthorize("hasRole('ROLE_SUPER')")
    @ApiOperation(value = "根据租户ID查询菜单")
    @GetMapping("getMenuResourceByTenantId/{tenantId}")
    public List<TenantMenuResourceVO> getMenuResourceByTenantId(@PathVariable("tenantId") Long tenantId) {
        return iTenantMenuService.getMenuResourceByTenantId(tenantId);
    }
}